import { ajax, calcTimeHeader } from '../../utils/util.js'
let app = getApp()
Component({
  properties: {
    orderDetail: {
      type: Object,
      value: {}
    }
  },
  data: {
    anchorSkill: {},
    showModal: false
  },
  ready () {
    let self = this
    setTimeout( () => {
      if (self.data.orderDetail.status === 'undefined' || self.data.orderDetail.status == -1 || self.data.orderDetail.status == 100) {
        let anchorAccid = app.globalData['currentChatTo']
        self.getUserSkill(anchorAccid)
      }
    },100)
  },
  methods: {
    getUserSkill (anchorAccid) {
      ajax('order/getUserSkill', {
        accid: anchorAccid
      }).then(res => {
        if (res.code == 0) {
          let data = res.data
          this.setData({
            anchorSkill: data
          })
        } else {
          console.log("获取主播技能异常")
        }
      })
    },
    toOrder () {
      let anchorDetail = this.data.anchorSkill;
      let skill = JSON.stringify(anchorDetail.skill);
      wx.navigateTo({
        url: `../../pages/toOrder/toOrder?skill=${skill}&price=${anchorDetail.price}&sellerId=${anchorDetail.user.user_id}&avatar=${anchorDetail.user.avatar}&nick=${anchorDetail.user.nick}&userSkillId=${anchorDetail.user_skill_id}`,
      })
    },
    finishBtn () {
      this.setData({
        showModal: true
      })
    },
    confirm () {
      ajax('order/finish', {
        token: wx.getStorageSync('Cookie'),
        order_no: this.data.orderDetail.order_no
      }).then(res => {
        let data = res.data || {}
        if (res.code == 0) {
          data.create_time = calcTimeHeader(data.create_time)
          data.plan_time = calcTimeHeader(data.plan_time)
          console.log(`订单状态:${data.status}`)
          data.status = app.globalData['orderstatus'][data.status]
          this.setData({
            orderDetail: data
          })
        } else {
          wx.showToast({
            title: '网络开小差了',
            icon: 'none',
            duration: 1000
          })
        }
        this.setData({
          showModal: false
        })
      })
    },
    cancel () {
      this.setData({
        showModal: false
      })
    }
  }
})
